package com.base.listen;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;


public class ServerListenHandler  extends ChannelInboundHandlerAdapter{
	 /**
     * 收到数据时调用
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        
    	try {
            ServerListen.reciveMessage(ctx,msg.toString());
        } finally {
            // 抛弃收到的数据
            ReferenceCountUtil.release(msg);
        }      
    }
    
    /**
     * 当Netty由于IO错误或者处理器在处理事件时抛出异常时调用
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        // 当出现异常就关闭连接
        cause.printStackTrace();
        ctx.close();
    }
}
